package com.jt.demo3.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TimeAspect {
    @Pointcut("bean(userServiceImpl)")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
//        Class<?> targetClass = joinPoint.getTarget().getClass();
//        System.out.println("目标对象的类型是:" + targetClass);
//
//        String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
//        System.out.println("目标对象的名称是:" + declaringTypeName);
//
//        String methodName = joinPoint.getSignature().getName();
//        System.out.println("目标对象的方法名称是:" + methodName);
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        System.out.println("运行时间: " + (endTime - startTime) + "毫秒");

        Class<?> targetClass = joinPoint.getTarget().getClass();
        System.out.println("目标对象的类型是:" + targetClass);

        String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
        System.out.println("目标对象的名称是:" + declaringTypeName);

        String methodName = joinPoint.getSignature().getName();
        System.out.println("目标对象的方法名称是:" + methodName);
        return proceed;
    }
}
